<?php
namespace Admin\Controller;

use Common\Controller\AdminBaseController;

/**
 * 后台赔率控制器
 */
class LottryoddsController extends AdminBaseController
{
    /**
     * 首页
     */
    public function index()
    {
        //S('lottry_array',NULL);
        $lottry_array = S('lottry_array');
        //print_r($lottry_array);exit;
        if ($_GET['pan']) {
            $pan = $_GET['pan'];
        } else {
            $pan = 'A';
        }
        if ($_GET['c_id']) {
            $c_id = $_GET['c_id'];
        } else {
            $c_id = 1;
        }
        if ($_GET['w_id']) {
            $w_id = $_GET['w_id'];
        } else {
            $w_id = 1;
        }
        if ($_GET['cl_id']) {
            $cl_id = $_GET['cl_id'];
        } else {
            $cl_id = 1;
        }
        unset($tpl);
        unset($odds);
        if ($c_id == 12 && ($w_id == 1 || $w_id == 7 || $w_id == 12)) {
            $odds = S($pan . '_odds_' . $c_id . '_' . $w_id . '_' . $cl_id . '');
        } else {
            $odds = S($pan . '_odds_' . $c_id . '_' . $w_id . '');
        }
        // S($pan.'_odds_'.$c_id.'_'.$w_id.'',NULL);

        if (empty($odds)) {
            $condition['c_id'] = $c_id;
            $condition['w_id'] = $w_id;
            $condition['pan']  = $pan;
            if ($c_id == 12 && ($w_id == 1 || $w_id == 7 || $w_id == 12)) {
                $condition['cl_id'] = $cl_id;
            }
            $odd  = M('lottery_odds')->where($condition)->getField('odds');
            $odds = unserialize($odd);
            if ($c_id == 12 && ($w_id == 1 || $w_id == 7 || $w_id == 12)) {
                S($pan . '_odds_' . $c_id . '_' . $w_id . '_' . $cl_id . '', $odds);
            } else {
                S($pan . '_odds_' . $c_id . '_' . $w_id . '', $odds);
            }
        }

        switch ($c_id) {
            //重庆时时彩
            case 1:
            case 23:
            case 24:
                switch ($w_id) {
                    case 1:
                        $tpl = "Odds/cqssc_ball";
                        break;
                    case 2:
                        $tpl = "Odds/cqssc_ball";
                        break;
                    case 3:
                        $tpl = "Odds/cqssc_ball";
                        break;
                    case 4:
                        $tpl = "Odds/cqssc_ball";
                        break;
                    case 5:
                        $tpl = "Odds/cqssc_ball";
                        break;
                    case 6:
                        $tpl = "Odds/cqssc_sum";
                        break;
                    case 7:
                        $tpl = "Odds/cqssc_three_ball";
                        break;
                    case 8:
                        $tpl = "Odds/cqssc_three_ball";
                        break;
                    case 9:
                        $tpl = "Odds/cqssc_three_ball";
                        break;
                    default:
                        break;
                }
                break;
            //新疆时时彩
            case 2:
                switch ($w_id) {
                    case 1:
                        $tpl = "Odds/xjssc_ball";
                        break;
                    case 2:
                        $tpl = "Odds/xjssc_ball";
                        break;
                    case 3:
                        $tpl = "Odds/xjssc_ball";
                        break;
                    case 4:
                        $tpl = "Odds/xjssc_ball";
                        break;
                    case 5:
                        $tpl = "Odds/xjssc_ball";
                        break;
                    case 6:
                        $tpl = "Odds/xjssc_sum";
                        break;
                    case 7:
                        $tpl = "Odds/xjssc_three_ball";
                        break;
                    case 8:
                        $tpl = "Odds/xjssc_three_ball";
                        break;
                    case 9:
                        $tpl = "Odds/xjssc_three_ball";
                        break;
                    default:
                        break;
                }
                break;
            //天津时时彩
            case 3:
                switch ($w_id) {
                    case 1:
                        $tpl = "Odds/tjssc_ball";
                        break;
                    case 2:
                        $tpl = "Odds/tjssc_ball";
                        break;
                    case 3:
                        $tpl = "Odds/tjssc_ball";
                        break;
                    case 4:
                        $tpl = "Odds/tjssc_ball";
                        break;
                    case 5:
                        $tpl = "Odds/tjssc_ball";
                        break;
                    case 6:
                        $tpl = "Odds/tjssc_sum";
                        break;
                    case 7:
                        $tpl = "Odds/tjssc_three_ball";
                        break;
                    case 8:
                        $tpl = "Odds/tjssc_three_ball";
                        break;
                    case 9:
                        $tpl = "Odds/tjssc_three_ball";
                        break;
                    default:
                        break;
                }
                break;
            //北京赛车PK10
            case 4:
                switch ($w_id) {
                    case 1:
                        $tpl = "Odds/bjpk10_sum";
                        break;
                    case 2:
                        $tpl = "Odds/bjpk10_sort";
                        break;
                    case 3:
                        $tpl = "Odds/bjpk10_sort";
                        break;
                    case 4:
                        $tpl = "Odds/bjpk10_sort";
                        break;
                    case 5:
                        $tpl = "Odds/bjpk10_sort";
                        break;
                    case 6:
                        $tpl = "Odds/bjpk10_sort";
                        break;
                    case 7:
                        $tpl = "Odds/bjpk10_sort1";
                        break;
                    case 8:
                        $tpl = "Odds/bjpk10_sort1";
                        break;
                    case 9:
                        $tpl = "Odds/bjpk10_sort1";
                        break;
                    case 10:
                        $tpl = "Odds/bjpk10_sort1";
                        break;
                    case 11:
                        $tpl = "Odds/bjpk10_sort1";
                        break;
                    default:
                        break;
                }
                break;
            //幸运飞艇
            case 5:
                switch ($w_id) {
                    case 1:
                        $tpl = "Odds/xyft_sum";
                        break;
                    case 2:
                        $tpl = "Odds/xyft_sort";
                        break;
                    case 3:
                        $tpl = "Odds/xyft_sort";
                        break;
                    case 4:
                        $tpl = "Odds/xyft_sort";
                        break;
                    case 5:
                        $tpl = "Odds/xyft_sort";
                        break;
                    case 6:
                        $tpl = "Odds/xyft_sort";
                        break;
                    case 7:
                        $tpl = "Odds/xyft_sort1";
                        break;
                    case 8:
                        $tpl = "Odds/xyft_sort1";
                        break;
                    case 9:
                        $tpl = "Odds/xyft_sort1";
                        break;
                    case 10:
                        $tpl = "Odds/xyft_sort1";
                        break;
                    case 11:
                        $tpl = "Odds/xyft_sort1";
                        break;
                    default:
                        break;
                }
                break;
            //广东快乐十分
            case 6:
                switch ($w_id) {
                    case 1:
                        $tpl = "Odds/gdkl_sum";
                        break;
                    case 2:
                        $tpl = "Odds/gdkl_ball";
                        break;
                    case 3:
                        $tpl = "Odds/gdkl_ball";
                        break;
                    case 4:
                        $tpl = "Odds/gdkl_ball";
                        break;
                    case 5:
                        $tpl = "Odds/gdkl_ball";
                        break;
                    case 6:
                        $tpl = "Odds/gdkl_ball1";
                        break;
                    case 7:
                        $tpl = "Odds/gdkl_ball1";
                        break;
                    case 8:
                        $tpl = "Odds/gdkl_ball1";
                        break;
                    case 9:
                        $tpl = "Odds/gdkl_ball1";
                        break;
                    case 10:
                        $tpl = "Odds/gdkl_zm";
                        break;
                    case 11:
                        $tpl = "Odds/gdkl_lm";
                        break;
                    default:
                        break;
                }
                break;
            //广西快乐三，湖北快乐三，河北快乐三，江苏快乐三
            case 7:
            case 18:
            case 19:
            case 20:
                switch ($w_id) {
                    case 1:
                        $tpl = "Odds/gxks_sd"; //三军
                        break;
                    case 2:
                        $tpl = "Odds/gxks_wq"; //围骰/全骰
                        break;
                    case 3:
                        $tpl = "Odds/gxks_ds"; //点数
                        break;
                    case 4:
                        $tpl = "Odds/gxks_cp"; //长牌
                        break;
                    case 5:
                        $tpl = "Odds/gxks_dp"; //短牌
                        break;
                    case 6:
                        $tpl = "Odds/gxks_f";
                        break;

                    default:
                        break;
                }
                break;
            //重庆幸运农场
            case 8:
                switch ($w_id) {
                    case 1:
                        $tpl = "Odds/cqnc_sum";
                        break;
                    case 2:
                        $tpl = "Odds/cqnc_ball";
                        break;
                    case 3:
                        $tpl = "Odds/cqnc_ball";
                        break;
                    case 4:
                        $tpl = "Odds/cqnc_ball";
                        break;
                    case 5:
                        $tpl = "Odds/cqnc_ball";
                        break;
                    case 6:
                        $tpl = "Odds/cqnc_ball1";
                        break;
                    case 7:
                        $tpl = "Odds/cqnc_ball1";
                        break;
                    case 8:
                        $tpl = "Odds/cqnc_ball1";
                        break;
                    case 9:
                        $tpl = "Odds/cqnc_ball1";
                        break;
                    case 10:
                        $tpl = "Odds/cqnc_zm";
                        break;
                    case 11:
                        $tpl = "Odds/cqnc_lm";
                        break;
                    default:
                        break;
                }
                break;
            //北京快乐8
            //台湾宾果
            //加拿大3.5
            case 9:
            case 15:
            case 22:
                switch ($w_id) {
                    case 1:
                        $tpl = "Odds/bjkl_ball";
                        break;
                    case 2:
                        $tpl = "Odds/bjkl_sum";
                        break;
                    case 3:
                        $tpl = "Odds/bjkl_fb";
                        break;
                    case 4:
                        $tpl = "Odds/bjkl_ds";
                        break;
                    case 5:
                        $tpl = "Odds/bjkl_wx";
                        break;

                    default:
                        break;
                }
                break;
            //广西快乐十分
            case 10:
                switch ($w_id) {
                    case 1:
                        $tpl = "Odds/gxkl10_sum";
                        break;
                    case 2:
                        $tpl = "Odds/gxkl10_ball";
                        break;
                    case 3:
                        $tpl = "Odds/gxkl10_ball";
                        break;
                    case 4:
                        $tpl = "Odds/gxkl10_ball";
                        break;
                    case 5:
                        $tpl = "Odds/gxkl10_ball";
                        break;
                    case 6:
                        $tpl = "Odds/gxkl10_ball";
                        break;
                    default:
                        break;
                }
                break;
            //PC蛋蛋
            case 11:
                switch ($w_id) {
                    case 1:
                        $tpl = "Odds/pcdd_sum";
                        break;
                    case 2:
                        $tpl = "Odds/pcdd_two";
                        break;
                    case 3:
                        $tpl = "Odds/pcdd_all";
                        break;

                    default:
                        break;
                }
                break;
            //六合彩
            case 12:
                switch ($w_id) {
                    case 1:
                        $tpl = "Odds/hk_ta";
                        break;
                    case 2:
                        $tpl = "Odds/hk_lm";
                        break;
                    case 3:
                        $tpl = "Odds/hk_sb";
                        break;
                    case 4:
                        $tpl = "Odds/hk_txtw";
                        break;
                    case 5:
                        $tpl = "Odds/hk_hx";
                        break;
                    case 6:
                        $tpl = "Odds/hk_zm";
                        break;
                    case 7:
                        $tpl = "Odds/hk_ztm";
                        break;
                    case 8:
                        $tpl = "Odds/hk_wx";
                        break;
                    case 9:
                        $tpl = "Odds/hk_yxzx";
                        break;
                    case 10:
                        $tpl = "Odds/hk_zx";
                        break;
                    case 11:
                        $tpl = "Odds/hk_zxbz";
                        break;
                    case 12:
                        if ($cl_id <= 4) {
                            $tpl = "Odds/hk_lxlw1"; //连肖
                        } else {
                            $tpl = "Odds/hk_lxlw2"; //连尾
                        }
                        break;
                    case 13:
                        $tpl = "Odds/hk_lmm";
                        break;
                    default:
                        break;
                }
                break;
            //分分彩
            case 13:
            case 16:
            case 17:
                switch ($w_id) {
                    case 1:
                        $tpl = "Odds/cqssc_ball";
                        break;
                    case 2:
                        $tpl = "Odds/cqssc_ball";
                        break;
                    case 3:
                        $tpl = "Odds/cqssc_ball";
                        break;
                    case 4:
                        $tpl = "Odds/cqssc_ball";
                        break;
                    case 5:
                        $tpl = "Odds/cqssc_ball";
                        break;
                    case 6:
                        $tpl = "Odds/cqssc_sum";
                        break;
                    case 7:
                        $tpl = "Odds/cqssc_three_ball";
                        break;
                    case 8:
                        $tpl = "Odds/cqssc_three_ball";
                        break;
                    case 9:
                        $tpl = "Odds/cqssc_three_ball";
                        break;
                    default:
                        break;
                }
                break;
            //上海时时乐
            case 21:
                switch ($w_id) {
                    case 1:
                        $tpl = "Odds/shssl_1z";
                        break;
                    case 2:
                        $tpl = "Odds/shssl_2z";
                        break;
                    case 3:
                        $tpl = "Odds/shssl_3z";
                        break;
                    case 4:
                        $tpl = "Odds/shssl_1zdw";
                        break;
                    case 5:
                        $tpl = "Odds/shssl_2zhs";
                        break;
                    case 6:
                        $tpl = "Odds/shssl_13zhs";
                        break;
                    case 7:
                        $tpl = "Odds/shssl_23zhs";
                        break;
                    case 8:
                        $tpl = "Odds/shssl_123zhs";
                        break;
                    case 9:
                        $tpl = "Odds/shssl_kd";
                        break;

                        break;
                }
            default:
                break;

        }

        $this->assign('lottry_array', $lottry_array);
        $this->assign('lottry_array_c', $lottry_array[$c_id]);
        $this->assign('c_id', $c_id);
        $this->assign('w_id', $w_id);
        $this->assign('cl_id', $cl_id);
        $this->assign('pan', $pan);
        $this->assign('odds', $odds);
        $this->assign('tpl', $tpl);
        $this->display();
    }

    public function change_odds()
    {

        $old_odds = $_POST['odds'];
        ksort($old_odds);
        $odds = serialize($old_odds);
        //print_r($odds);exit;
        $c_id              = $_POST['c_id'];
        $w_id              = $_POST['w_id'];
        $cl_id             = $_POST['cl_id'];
        $pan               = $_POST['pan'];
        $condition['c_id'] = $c_id;
        $condition['w_id'] = $w_id;
        $condition['pan']  = $pan;
        if ($c_id == 12 && ($w_id == 1 || $w_id == 7 || $w_id == 12)) {
            $condition['cl_id'] = $cl_id;
        }

        $update_array['odds'] = $odds;
        $result               = M('lottery_odds')->where($condition)->save($update_array);
        if ($result >= 0) {

            if ($c_id == 12 && ($w_id == 1 || $w_id == 7 || $w_id == 12)) {
                S($pan . '_odds_' . $c_id . '_' . $w_id . '_' . $cl_id . '', null);
            } else {
                S($pan . '_odds_' . $c_id . '_' . $w_id . '', null);
            }

            S($pan . '_odds_' . $c_id . '_' . $w_id . '', null);
            admin_log('编辑赔率 盘口:' . $pan . ',采种' . $c_id . '/玩法' . $w_id . '');
            $this->success('修改赔率成功', U('Admin/Lottryodds/index', array('pan' => $pan, 'c_id' => $c_id, 'w_id' => $w_id)));exit;
        } else {
            $this->error('修改赔率失败', U('Admin/Lottryodds/index', array('pan' => $pan, 'c_id' => $c_id, 'w_id' => $w_id)));exit;
        }

    }
    //反水参数设置
    public function return_setting()
    {
        $lottry_array = S('lottry_array');

        if ($_GET['c_id']) {
            $c_id = $_GET['c_id'];
        } else {
            $c_id = 1;
        }
        unset($rebate);
        $rebate = S('rebate_' . $c_id . '');

        if (empty($rebate)) {
            $condition['c_id'] = $c_id;
            $rebate            = M('rebate_setting')->where($condition)->find();
            //print_r($rebate);exit;
            S('rebate_' . $c_id . '', $rebate);
        }
        $this->assign('rebate', $rebate);
        $this->assign('lottry_array', $lottry_array);
        $this->assign('c_id', $c_id);
        $this->display();
    }
    public function change_rebate()
    {

        $c_id                       = $_POST['c_id'];
        $condition['c_id']          = $c_id;
        $update_array['percentage'] = $_POST['percentage'];
        $update_array['status']     = $_POST['status'];
        $result                     = M('rebate_setting')->where($condition)->save($update_array);
        if ($result >= 0) {
            S('rebate_' . $c_id . '', null);
            admin_log('编辑反水参数 采种' . $c_id . '');
            $this->success('修改赔率成功', U('Admin/Lottryodds/return_setting', array('c_id' => $c_id)));exit;
        } else {
            $this->error('修改赔率失败', U('Admin/Lottryodds/return_setting', array('c_id' => $c_id)));exit;
        }
    }

    //反水参数设置
    public function dl_return_setting()
    {

        $rebate2 = M('dl_rebate_setting')->select();

        $this->assign('rebate', $rebate2);

        $this->display();
    }
    public function change_dl_rebate()
    {

        for ($i = 0; $i < 6; $i++) {
            $ii = $i + 1;

            $condition['c_id'] = 1;
            $condition['id']   = $ii;

            $update_array['percentage']      = $_POST['percentage'][$i];
            $update_array['lost_percentage'] = $_POST['lost_percentage'][$i];
            $update_array['amount']          = $_POST['amount'][$i];

            $result = M('dl_rebate_setting')->where($condition)->save($update_array);
        }
        if ($result >= 0) {
            S($pan . '_dl_rebate_' . $c_id . '', null);
            admin_log('编辑代理反水参数 采种' . $c_id . '');
            $this->success('修改代理反水参数成功', U('Admin/Lottryodds/dl_return_setting', array('pan' => $pan, 'c_id' => $c_id)));exit;
        } else {
            $this->error('修改代理反水参数失败', U('Admin/Lottryodds/dl_return_setting', array('pan' => $pan, 'c_id' => $c_id)));exit;
        }
    }

//开奖结果
    public function lottery_results()
    {
        $lottry_array = S('lottry_array');

        if ($_GET['c_id']) {
            $c_id = $_GET['c_id'];
        } else {
            $c_id = 1;
        }
        $where['c_id'] = $c_id;
        if (empty($_GET['strtime'])) {
            $_GET['strtime'] = date('m/d/Y', time());
        }

        $begin             = strtotime($_GET['strtime']);
        $end               = $begin + 86400;
        $where['add_time'] = array('between', array($begin, $end));

        $count = M('lottery_results')->where($where)->count();

        $Page = new_page($count, 20); // 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show = $Page->show(); // 分页显示输出
        $list = M('lottery_results')->where($where)->limit($Page->firstRow . ',' . $Page->listRows)->order('id desc')->select();
        $this->assign('strtime', $_GET['strtime']);
        $this->assign('lottry_array', $lottry_array);
        $this->assign('c_id', $c_id);
        $this->assign('page', $show);
        switch ($c_id) {
            case 1:
            case 2:
            case 3:
            case 24:
            case 23:
                foreach ($list as $k => $v) {
                    $ball             = explode(',', $v['lottery_result']);
                    $list[$k]['ball'] = $ball;
                    $list[$k]['sum']  = array_sum($ball);
                    if ($ball[0] > $ball[4]) {
                        $list[$k]['lh'] = '龙';
                    } elseif ($ball[0] < $ball[4]) {
                        $list[$k]['lh'] = '虎';
                    } else {
                        $list[$k]['lh'] = '和';
                    }

                    $list[$k]['three'] = $this->three($ball);

                }
                $this->assign('data', $list);
                $this->display('ssc_results'); //时时彩(重庆 天津 新疆)
                break;
            case 4:
            case 5:
                foreach ($list as $k => $v) {
                    $ball             = explode(',', $v['lottery_result']);
                    $list[$k]['ball'] = $ball;
                    $list[$k]['sum']  = $ball[0] + $ball[1];

                }
                $this->assign('data', $list);
                $this->display('pk10_results'); //北京PK10 幸运飞艇
                break;
            case 6:
            case 8:
                foreach ($list as $k => $v) {
                    $ball             = explode(',', $v['lottery_result']);
                    $list[$k]['ball'] = $ball;
                    $list[$k]['sum']  = array_sum($ball);

                }
                $this->assign('data', $list);
                $this->display('gdkl_results'); //广东快乐十分 幸运农场
                break;
            case 7:
            case 18:
            case 19:
            case 20:
                foreach ($list as $k => $v) {
                    $ball             = explode(',', $v['lottery_result']);
                    $list[$k]['ball'] = $ball;
                    $list[$k]['sum']  = array_sum($ball);

                }
                $this->assign('data', $list);
                $this->display('gxk3_results'); //广东快乐十分 幸运农场
                break;
            case 9:
            case 15:
            case 22:
                foreach ($list as $k => $v) {
                    $ball             = explode(',', $v['lottery_result']);
                    $list[$k]['ball'] = $ball;
                    $list[$k]['sum']  = array_sum($ball);

                }
                $this->assign('data', $list);
                $this->display('bjk8_results'); //广东快乐十分 幸运农场
                break;
            case 10:
                foreach ($list as $k => $v) {
                    $ball             = explode(',', $v['lottery_result']);
                    $list[$k]['ball'] = $ball;
                    $list[$k]['sum']  = array_sum($ball);

                }
                $this->assign('data', $list);
                $this->display('gxkl_results'); //广东快乐十分 幸运农场
                break;
            case 11:
                foreach ($list as $k => $v) {
                    $ball             = explode(',', $v['lottery_result']);
                    $list[$k]['ball'] = $ball;
                    $list[$k]['sum']  = array_sum($ball);

                }
                $this->assign('data', $list);
                $this->display('pcdd_results'); //广东快乐十分 幸运农场
                break;
            case 12:
                foreach ($list as $k => $v) {
                    $ball             = explode(',', $v['lottery_result']);
                    $list[$k]['ball'] = $ball;
                    $list[$k]['sum']  = array_sum($ball);

                }
                $this->assign('data', $list);
                $this->display('hk6_results'); //六合彩
                break;
            case 13:
            case 16:
            case 17:
                foreach ($list as $k => $v) {
                    $ball             = explode(',', $v['lottery_result']);
                    $list[$k]['ball'] = $ball;
                    $list[$k]['sum']  = array_sum($ball);
                    if ($ball[0] > $ball[4]) {
                        $list[$k]['lh'] = '龙';
                    } elseif ($ball[0] < $ball[4]) {
                        $list[$k]['lh'] = '虎';
                    } else {
                        $list[$k]['lh'] = '和';
                    }

                    $list[$k]['three'] = $this->three($ball);

                }
                $r_id = $_GET['id'];
                if ($r_id) {
                    $r_array                   = M('lottery_results')->where('id=' . $r_id)->find();
                    $r_array['add_time']       = date('Y-m-d H:i:s', $r_array['add_time']);
                    $r_array['lottery_result'] = (explode(",", $r_array['lottery_result']));
                    $this->assign('r_array', $r_array);
                }
                $this->assign('data', $list);

                $this->assign('o_id', $r_id);
                $this->display('ffc_results'); //分分彩 幸运农场
                break;

            default:$this->display();
                break;
        }

    }
    public function insert_result()
    {
        $c_id = $_GET['c_id'];
        if ($c_id) {
            $o_id = $_GET['o_id'];
            if ($o_id) {
                //编辑
                if (empty($_GET['c_no'])) {
                    $this->error('期号不能为空', U('Admin/Lottryodds/lottery_results', array('c_id' => $c_id)));exit;
                }
                $is_have = M('lottery_results')->where('id!=' . $_GET['o_id'] . ' and c_no=' . $_GET['c_no'])->select();
                if (!empty($is_have)) {
                    $this->error('期号重复', U('Admin/Lottryodds/lottery_results', array('c_id' => $c_id)));exit;
                }

                $update_array                   = array();
                $update_array['c_no']           = $_GET['c_no'];
                $update_array['add_time']       = strtotime($_GET['add_time']);
                $update_array['lottery_result'] = implode(",", $_GET['lottery_result']);
                $ffc_result                     = new \Org\Util\Ffcresult();
                $result_lm                      = $ffc_result->getffclm($_GET['c_no'], $_GET['lottery_result']);
                $update_array['lm']             = $result_lm['lm'];
                $update_array['detail']         = $result_lm['detail'];

                $result = M('lottery_results')->where('id=' . $_GET['o_id'] . ' and c_id=' . $_GET['c_id'])->save($update_array);
                if ($result >= 0) {
                    $this->success('修改成功', U('Admin/Lottryodds/lottery_results', array('c_id' => $c_id)));exit;
                } else {
                    $this->error('修改错误', U('Admin/Lottryodds/lottery_results', array('c_id' => $c_id)));exit;
                }
            } else {
                //插入
                if (empty($_GET['c_no'])) {
                    $this->error('期号不能为空', U('Admin/Lottryodds/lottery_results', array('c_id' => $c_id)));exit;
                }
                $is_have = M('lottery_results')->where('c_id=13 and c_no=' . $_GET['c_no'])->select();
                if (!empty($is_have)) {
                    $this->error('期号重复', U('Admin/Lottryodds/lottery_results', array('c_id' => $c_id)));exit;
                }
                $insert_array                   = array();
                $insert_array['c_id']           = 13;
                $insert_array['c_no']           = $_GET['c_no'];
                $insert_array['c_name']         = '分分彩';
                $insert_array['add_time']       = strtotime($_GET['add_time']);
                $insert_array['lottery_result'] = implode(",", $_GET['lottery_result']);
                $ffc_result                     = new \Org\Util\Ffcresult();
                $result_lm                      = $ffc_result->getffclm($_GET['c_no'], $_GET['lottery_result']);
                $insert_array['lm']             = $result_lm['lm'];
                $insert_array['detail']         = $result_lm['detail'];
                $insert_array['status']         = 0;
                $insert_result                  = M('lottery_results')->add($insert_array);
                if ($insert_result) {
                    $this->success('添加成功', U('Admin/Lottryodds/lottery_results', array('c_id' => $c_id)));exit;
                } else {
                    $this->error('添加错误', U('Admin/Lottryodds/lottery_results', array('c_id' => $c_id)));exit;
                }
            }
            // isset();
        } else {
            $this->error('参数错误', U('Admin/Lottryodds/lottery_results', array('c_id' => $c_id)));exit;
        }
    }

    public function Jsffc()
    {
        $ffc_result = new \Org\Util\Jsffc();
        $result     = $ffc_result->balance($_GET['c_no']);
        if ($result == 1) {
            $this->success('结算成功', U('Admin/Lottryodds/lottery_results', array('c_id' => $_GET['c_id'])));exit;
        } else {
            $this->error('结算错误', U('Admin/Lottryodds/lottery_results', array('c_id' => $_GET['c_id'])));exit;
        }
    }
    public function get_array()
    {
        $kj_time = strtotime('10:00:00');
        $fp_time = strtotime('9:59:00');
        $kp_time = strtotime('9:30:00');
        //$time=date('Y-m-d H:i:s',$time);
        $data = array();
        for ($i = 1; $i <= 23; $i++) {
            $data[$i]['periods'] = $i;
            $kj_time             = $kj_time + 1800;
            $fp_time             = $fp_time + 1800;
            $kp_time             = $kp_time + 1800;
            $kj_time2            = date('H:i:s', $kj_time);
            $fp_time2            = date('H:i:s', $fp_time);
            $kp_time2            = date('H:i:s', $kp_time);
            $data[$i]['kp_time'] = $kp_time2;
            $data[$i]['fp_time'] = $fp_time2;
            $data[$i]['kj_time'] = $kj_time2;
        }
        print_r(serialize($data));exit;
    }
//开奖时间设置
    public function open_time()
    {

        //       $kp_time=strtotime('08:53:30');
        //       $fp_time=strtotime('09:02:30');

        //       $kj_time=strtotime('09:03:30');

        //       $data=array();
        // for($i=1;$i<=84;$i++){
        //     $data[$i]['periods']=$i;
        //     $kj_time=$kj_time+600;
        //     $fp_time=$fp_time+600;
        //     $kp_time=$kp_time+600;
        //     $kj_time2=date('H:i:s',$kj_time);
        //     $fp_time2=date('H:i:s',$fp_time);
        //     $kp_time2=date('H:i:s',$kp_time);
        //     $data[$i]['kp_time']=$kp_time2;
        //     $data[$i]['fp_time']=$fp_time2;
        //     $data[$i]['kj_time']=$kj_time2;
        //     }
        //       print_r(serialize($data));exit;

        if ($_GET['c_id']) {
            $c_id = $_GET['c_id'];
        } else {
            $c_id = 1;
        }
        $lottry_array = S('lottry_array');

        unset($open_time);
        $open_time = S('op_time_' . $c_id . '');

        if (empty($open_time)) {
            $condition['c_id'] = $c_id;
            $op_time           = M('open_time')->where($condition)->getField('data');
            $open_time         = unserialize($op_time);

            S('op_time_' . $c_id . '', $open_time);
        }
        $count = count($open_time);
        S('op_time_' . $c_id . '', null);
        // print_r($open_time);exit;
        $this->assign('count', $count);
        $this->assign('data', $open_time);
        $this->assign('lottry_array', $lottry_array);
        $this->assign('c_id', $c_id);
        if ($c_id == 12) {
            //六合彩开奖时间设置
            $this->display('open_time_hk');
        } else {
            $this->display();
        }

    }
    //修改开奖时间
    public function change_op_time()
    {
        $c_id              = $_POST['c_id'];
        $condition['c_id'] = $c_id;

        $count = count($_POST['kaipan']);
        $data  = array();
        for ($i = 1; $i <= $count; $i++) {
            $data[$i]['periods'] = $i;
            $data[$i]['kp_time'] = $_POST['kaipan'][$i];
            $data[$i]['fp_time'] = $_POST['fengpan'][$i];
            $data[$i]['kj_time'] = $_POST['kaijiang'][$i];
        }
        //六合彩
        if ($c_id == 12) {
            $data = array();

            for ($i = 0; $i < 10; $i++) {
                $qi                  = $_POST['periods'][$i];
                $data[$i]['periods'] = $qi;
                $data[$i]['kp_time'] = strtotime($_POST['kp_time'][$i]);
                $data[$i]['fp_time'] = strtotime($_POST['fp_time'][$i]);
                $data[$i]['kj_time'] = strtotime($_POST['kj_time'][$i]);
                //计算开奖日期
                $weekday = array('星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六');

                $weekday             = $weekday[date('w', strtotime($_POST['kj_time'][$i]))];
                $data[$i]['weekday'] = $weekday;

            }

        }

        //print_r($_POST['periods']);exit;
        $ndata                = serialize($data);
        $update_array['data'] = $ndata;
        $result               = M('open_time')->where($condition)->save($update_array);
        if ($result >= 0) {
            S('op_time_' . $c_id . '', null);
            admin_log('编辑开奖时间参数 采种' . $c_id . '');
            $this->success('修改开奖时间成功', U('Admin/Lottryodds/open_time', array('c_id' => $c_id)));exit;
        } else {
            $this->error('修改开奖时间失败', U('Admin/Lottryodds/open_time', array('c_id' => $c_id)));exit;
        }

    }

    //下注无效，退款处理
    public function order_back()
    {
        $order_sn       = $_GET['oid'];
        $data['status'] = 10;
        $order          = M('lottery_order')->field('money,m_id')->where("order_sn='" . $order_sn . "' and status=0")->find();
        $result         = M('lottery_order')->where("order_sn='" . $order_sn . "' and status=0")->save($data);
        if ($result >= 0) {
            $money = $order['money'];
            $ret   = M('member')->where('member_id=' . $order['m_id'])->setInc('member_money', $money);
            if ($ret == 1) {
                admin_log('用户id:' . $order['m_id'] . ',订单号：' . $order_sn . ',无效处理，已退款。');
                $this->success('退款成功', U('Admin/Lottryodds/lottery_order', array('c_id' => $_GET['c_id'])));exit;
            } else {
                $this->error('操作错误', U('Admin/Lottryodds/lottery_order', array('c_id' => $_GET['c_id'])));exit;
            }
        }
    }
//下注管理
    public function lottery_order()
    {
        $lottry_array = S('lottry_array');
        $type         = S('type_array');
        if ($_GET['strtime'] || $_GET['endtime']) {
            $condition['strtime'] = $_GET['strtime'];
            $condition['endtime'] = $_GET['endtime'];
        }
        if ($_GET['member_name']) {
            $condition['m_id'] = M('member')->where(" member_name = '" . $_GET['member_name'] . "'")->getField('member_id');
        }
        if ($_GET['order_sn']) {
            $condition['order_sn'] = $_GET['order_sn'];
        }
        $condition['status'] = $_GET['status'];
        if ($_GET['c_id']) {
            $c_id = $_GET['c_id'];
        } else {
            $c_id = 1;
        }
        $condition['c_id'] = $c_id;

        $order = D('Lottrey')->getOrderList($condition, '20');
        foreach ($order['data'] as $k => $v) {
            $order['data'][$k]['member_name'] = M('member')->where(" member_id = '" . $v['m_id'] . "'")->getField('member_name');

            if (is_numeric($v['t_type'])) {
                $order['data'][$k]['content'] = $type[$v['t_type']];
            } else {
                $order['data'][$k]['content'] = $type[$v['w_type']][$v['t_type']];
            }

        }

        $this->assign('lottry_array', $lottry_array);
        $this->assign('c_id', $c_id);
        $this->assign('order', $order);
        $this->display();
    }
//代理反水列表
    public function dl_return_list()
    {

        $name     = I('member_name');
        $startime = strtotime(I('strtime'));
        $endtime  = strtotime(I('endtime'));
        if (!empty($name)) {
            $condition['member_name'] = array("like", "%" . $name . "%");
            $cx_id                    = M('Member')->field('member_id')->where($condition)->find();
            $map['m_id']              = $cx_id['member_id'];
        }

        $lottry_array = S('lottry_array');
        $Info         = M('dl_fanli');

        //$count =$Info->join('__MEMBER__ ON __DL_FANLI__.x_id =__MEMBER__.member_id','left')->where($map)->count();
        $count = $Info->where($map)->count();
        $Page  = new \Org\Nx\Page($count, 10);
        $show  = $Page->show();

        // $info =$Info->join('__MEMBER__ ON __DL_FANLI__.x_id =__MEMBER__.member_id','left')->where($map)->limit($Page->firstRow.','.$Page->listRows)->order('id desc')->select();
        $info = $Info->where($map)->limit($Page->firstRow . ',' . $Page->listRows)->order('id desc')->select();
        foreach ($info as $key => $value) {
            $m_info                    = M('Member')->field('member_name')->where('member_id=' . $value['m_id'])->find();
            $info[$key]['member_name'] = $m_info['member_name'];

        }
        $this->assign('lottry_array', $lottry_array);
        $this->assign('info', $info);
        $this->assign('page', $show);
        $this->display();
    }

    //代理反水单删
    public function lottryodds_del()
    {
        if (!isAJAX) {
            halt('请求有误');
        } else {
            $id = I('id');

            if (!empty($id)) {

                $data = M('Dl_fanli');
                $info = $data->delete($id);
                if ($info >= 0) {
                    $data = array('info' => '删除成功', 'status' => '1');
                } else {
                    $data = array('info' => '删除失败', 'status' => '0');
                }

            } else {
                $data = array('info' => '参数有误');
            }
            $this->ajaxReturn($data, 'json');
        }

    }
    //全部删除
    public function both_del()
    {

        $data = M('Dl_fanli');

        $id = $_POST['both_id'];
        //print_r($ac_id);exit;
        foreach ($id as $key => $val) {

            $info = $data->delete($val);

        }
        if ($info) {
            $this->success('删除消息完成', U('Admin/Lottryodds/index'));exit;
        } else {
            $this->error('删除消息失败', U('Admin/Lottryodds/index'));exit;
        }

    }
    //手动结算
    public function Js()
    {
        $type = $_GET['type'];
        if ($type == 1) {
            $hk6_result = new \Org\Util\Jshk6(); //香港六合彩
            $hk6_result->hk6_balance();
            $data['status'] = 1;
            $this->ajaxReturn($data);
            exit(0);
        } elseif ($type == 2) {
            $xjssc_result = new \Org\Util\Jsxjssc(); //新疆时时彩
            $xjssc_result->xjssc_balance();

            $tjssc_result = new \Org\Util\Jstjssc(); //天津时时彩
            $tjssc_result->tjssc_balance();

            $gxk3_result = new \Org\Util\Jsgxk3(); //广西快3
            $gxk3_result->gxk3_balance();

            $gxklsf_result = new \Org\Util\Jsgxklsf(); //广西快乐十分
            $gxklsf_result->gxklsf_balance();

            $cqnc_result = new \Org\Util\Jscqnc(); //重庆农场
            $cqnc_result->cqnc_balance();

            $gdklsf_result = new \Org\Util\Jsgdklsf(); //广东快乐十分
            $gdklsf_result->gdklsf_balance();

            $gdklsf_result = new \Org\Util\Jsgdklsf(); //广东快乐十分
            $gdklsf_result->gdklsf_balance();

            $cqssc_result = new \Org\Util\Jscqssc(); //重庆时时彩
            $cqssc_result->cqssc_balance();

            $bjpk_result = new \Org\Util\Jsbjpk(); //北京PK10
            $bjpk_result->bjpk_balance();

            $xyft_result = new \Org\Util\Jsxyft(); //幸运飞艇
            $xyft_result->xyft_balance();

            $bjk8_result = new \Org\Util\Jsbjk8(); //北京快8
            $bjk8_result->bjk8_balance();

            $pcdd_result = new \Org\Util\Jspcdd(); //PC蛋蛋
            $pcdd_result->pcdd_balance();

            $data['status'] = 1;
            $this->ajaxReturn($data);
        } elseif ($type == 3) {
            $ffc_result = new \Org\Util\Jsffc(); //分分彩
            $ffc_result->balance();

            $Jsqqffc = new \Org\Util\Jsqqffc(); //QQ分分彩
            $Jsqqffc->balance();
            $Jstxffc = new \Org\Util\Jstxffc(); //腾讯分分彩
            $Jstxffc->balance();
            $Jsjnd = new \Org\Util\Jsjnd(); //加拿大3.5
            $Jsjnd->balance();

            $data['status'] = 1;
            $this->ajaxReturn($data);
        }
        $this->display();
    }

    public function lottery_type()
    {

        $lottry = include APP_PATH . 'Common/Common/lottery.php';

        if ($_POST) {
            foreach ($lottry as $k => $v) {
                $sort[$k]['sort'] = $_POST['sort'][$k];
            }

            $data['sort'] = serialize($sort);
            M('lottery_sort')->where('id=1')->save($data);
            $this->success('排序成功', U('Admin/Lottryodds/lottery_type'));exit;
        }

        $sort     = M('lottery_sort')->find();
        $sort     = unserialize($sort['sort']);
        $sort_new = array();
        foreach ($sort as $key => $value) {
            $sort_new[] = $value['sort'];
        }

        foreach ($lottry as $k => $v) {
            $lottry[$k]['sort']       = $sort[$k]['sort'];
            $lottry[$k]['lottery_id'] = $k;
        }
        array_multisort($sort_new, SORT_ASC, $lottry);
        $this->assign('lottery', $lottry);
        $this->display();

    }

    public function three($ball)
    {
        $three = '';
        //前三
        $numberArray[0] = $ball[0];
        $numberArray[1] = $ball[1];
        $numberArray[2] = $ball[2];

        //计算顺子、对子、半顺、豹子、杂六
        $resultArray = $this->calcGamePlayResult($numberArray);
        if ($resultArray[0] == 1) {
            $three .= '对子/';
        } elseif ($resultArray[0] == 2) {
            $three .= '豹子/';
        } elseif ($resultArray[1] == 1) {
            $three .= '半顺/';
        } elseif ($resultArray[1] == 2) {
            $three .= '顺子/';
        } else {
            $three .= '杂六/';
        }
        //中三
        $numberArray[0] = $ball[1];
        $numberArray[1] = $ball[2];
        $numberArray[2] = $ball[3];
        //计算顺子、对子、半顺、豹子、杂六
        $resultArray = $this->calcGamePlayResult($numberArray);
        if ($resultArray[0] == 1) {
            $three .= '对子/';
        } elseif ($resultArray[0] == 2) {
            $three .= '豹子/';
        } elseif ($resultArray[1] == 1) {
            $three .= '半顺/';
        } elseif ($resultArray[1] == 2) {
            $three .= '顺子/';
        } else {
            $three .= '杂六/';
        }
        //后三
        $numberArray[0] = $ball[2];
        $numberArray[1] = $ball[3];
        $numberArray[2] = $ball[4];
        //计算顺子、对子、半顺、豹子、杂六
        $resultArray = $this->calcGamePlayResult($numberArray);
        if ($resultArray[0] == 1) {
            $three .= '对子';
        } elseif ($resultArray[0] == 2) {
            $three .= '豹子';
        } elseif ($resultArray[1] == 1) {
            $three .= '半顺';
        } elseif ($resultArray[1] == 2) {
            $three .= '顺子';
        } else {
            $three .= '杂六';
        }
        return $three;
    }
    /**
     * 计算顺子、对子、半顺、豹子、杂六
     *
     * @arguments array 数组
     *
     * @returns 数组下标0 对应豹子、对子   数组下标1 对应顺子、半顺、杂六
     */
    public function calcGamePlayResult($array)
    {
        sort($array);
        //存储计算结果
        $resultArray = array();

        //计算豹子、对子
        $resultArray[0] = $array[2] - $array[1] == 0 ? 1 : 0;
        $resultArray[0] = $array[1] - $array[0] == 0 ? ++$resultArray[0] : $resultArray[0];

        //计算顺子、半顺、杂六
        $resultArray[1] = $array[2] - $array[1] == 1 ? 1 : 0;
        $resultArray[1] = $array[1] - $array[0] == 1 ? ++$resultArray[1] : $resultArray[1];

        return $resultArray;
    }

}
